debugger
let label = $(泵站选择.id).combobox('getValue');
let text = $(泵站选择.id).combobox('getText');
let tagMap = {
    "1": ["CB3_IPS_COD_QU","CB3_IPS_TP_QU","CB3_IPS_TN_QU","CB3_IPS_NH3N_QU","CB3_DFP_COD_QU","CB3_DFP_PH_QU","CB3_DFP_NH3N_QU","CB3_DFP_SS_QU","CB3_DFP_TIT_QU","CB3_DFP_TP_QU","CB3_DFP_TN_QU","CB3_DFP_LIT_QU","CB3_ASS_OUT_PH_QU","CB3_DFP_LIT_Accu_ACCU"],
    "2": ["CB3_IPS_CSR_F_LIT_QU","CB3_IPS_CSR_B_LIT_QU","CB3_IPS_WCT_LIT_QU","CB3_IPS_COD_QU","CB3_IPS_TP_QU","CB3_IPS_TN_QU","CB3_IPS_NH3N_QU"],
    "3": ["CB3_JHJ_SYWN_ACCU_FIT_ACCU","CB3_CSR_F_LIT_QU","CB3_IPS_CSR_B_LIT_QU","CB3_AER01_HLWN_ACCU_FIT_ACCU","CB3_JHJ_WN_LIT_QU"],
    "4": ["CB3_AER01_DO01_QU","CB3_AER01_DO02_QU","CB3_AER01_FIT_BQ_QU","CB3_AER01_MLSS_QU","CB3_AER01_N03N_QU","CB3_AER01_ORP01_QU","CB3_AER01_ORP02_QU","CB3_AER02_DO01_QU","CB3_AER02_DO02_QU","CB3_AER02_FIT_BQ_QU","CB3_AER02_MLSS_QU","CB3_AER02_N03N_QU","CB3_AER02_ORP01_QU","CB3_AER02_ORP02_QU","CB3_AER01_HLWN_FIT_QU","CB3_AER01_FIT_IN_QU","CB3_AER01_FIT_ACCU_IN_ACCU"],
    "5": ["CB3_AER03_DO01_QU","CB3_AER03_DO02_QU","CB3_AER03_FIT_BQ_QU","CB3_AER03_MLSS_QU","CB3_AER03_N03N_QU","CB3_AER03_ORP01_QU","CB3_AER03_ORP02_QU","CB3_AER04_DO01_QU","CB3_AER04_DO02_QU","CB3_AER04_FIT_BQ_QU","CB3_AER04_MLSS_QU","CB3_AER04_N03N_QU","CB3_AER04_ORP01_QU","CB3_AER04_ORP02_QU","CB3_AER03_HLWN_FIT_QU","CB3_AER03_FIT_IN_QU","CB3_AER03_FIT_ACCU_IN_ACCU"],
    "6": ["CB3_BH_RBL_B01_SpeedAct","CB3_BH_RBL_B01_Current","CB3_BH_RBL_B01_Temperature","CB3_BH_RBL_B02_SpeedAct","CB3_BH_RBL_B02_Current","CB3_BH_RBL_B02_Temperature","CB3_BH_RBL_B03_RunPercent","CB3_BH_RBL_B03_Current","CB3_BH_RBL_B03_MotorTemperature","CB3_BH_RBL_B04_SpeedAct","CB3_BH_RBL_B04_Current","CB3_BH_RBL_B04_Temperature","CB3_BH_BQZ_FIT_QU","CB3_BH_BQ_PIT_QU","CB3_DFP_COD_QU","CB3_DFP_PH_QU","CB3_DFP_NH3N_QU","CB3_DFP_SS_QU","CB3_DFP_TIT_QU","CB3_DFP_TP_QU","CB3_DFP_TN_QU","CB3_DFP_LIT_Accu_ACCU"],
    "7": ["CB3_LPS_TP_QU","CB3_LPS_N03N_QU","CB3_LPS_LIT_QU"],
    "8": ["CB3_DFP_RCP_LIT_QU","CB3_CMF_01_Level","CB3_CMF_02_Level","CB3_CMF_03_Level","CB3_CMF_04_Level","CB3_CMF_05_Level","CB3_CMF_06_Level"],
    "9": ["CB3_HST_HLWN_01_FIT_QU","CB3_HST_HLWN_02_FIT_QU","CB3_HST_SYWN_FIT_QU"],
    "10": ["CB3_PAC_CG_LIT_QU","CB3_PAC_Accu_FIT_ACCU","CB3_NaCLO_CG_LIT_QU","CB3_NaCLO_Accu_FIT_ACCU","CB3_CAD_CG_LIT_QU","CB3_CAD_Accu_FIT_ACCU"],
    "11": ["CB3_IPS_CSR_M01_QRUNING","CB3_IPS_CSR_M02_QRUNING","CB3_IPS_SBP_P01_QRUNING","CB3_IPS_SBP_P02_QRUNING","CB3_IPS_SBP_P03_QRUNING","CB3_IPS_SBP_P04_QRUNING","CB3_IPS_SBP_P05_QRUNING"],
    "12": ["CB3_CSR_FSR_M01_QRUNING","CB3_CSR_FSR_M02_QRUNING","CB3_ASS_RBL_B01_QRUNING","CB3_ASS_RBL_B02_QRUNING","CB3_ASS_SAB_B01_QRUNING","CB3_ASS_SAB_B02_QRUNING","CB3_JHJ_RSP_P01_QRUNING","CB3_JHJ_RSP_P02_QRUNING","CB3_JHJ_RSP_P03_QRUNING","CB3_JHJ_RSP_P04_QRUNING","CB3_JHJ_ESP_P01_QRUNING","CB3_JHJ_ESP_P02_QRUNING","CB3_JHJ_ESP_P03_QRUNING"],
    "13": ["CB3_AER01_PUS_M01_QRUNING","CB3_AER01_PUS_M02_QRUNING","CB3_AER01_PUS_M03_QRUNING","CB3_AER01_PUS_M04_QRUNING","CB3_AER01_PUS_M05_QRUNING","CB3_AER01_PUS_M06_QRUNING","CB3_AER01_IRP_P01_QRUNING","CB3_AER01_IRP_P02_QRUNING","CB3_AER02_PUS_M01_QRUNING","CB3_AER02_PUS_M02_QRUNING","CB3_AER02_PUS_M03_QRUNING","CB3_AER02_PUS_M04_QRUNING","CB3_AER02_PUS_M05_QRUNING","CB3_AER02_PUS_M06_QRUNING","CB3_AER02_IRP_P01_QRUNING","CB3_AER02_IRP_P02_QRUNING"],
    "14": ["CB3_AER03_PUS_M01_QRUNING","CB3_AER03_PUS_M02_QRUNING","CB3_AER03_PUS_M03_QRUNING","CB3_AER03_PUS_M04_QRUNING","CB3_AER03_PUS_M05_QRUNING","CB3_AER03_PUS_M06_QRUNING","CB3_AER03_IRP_P01_QRUNING","CB3_AER03_IRP_P02_QRUNING","CB3_AER04_PUS_M01_QRUNING","CB3_AER04_PUS_M02_QRUNING","CB3_AER04_PUS_M03_QRUNING","CB3_AER04_PUS_M04_QRUNING","CB3_AER04_PUS_M05_QRUNING","CB3_AER04_PUS_M06_QRUNING","CB3_AER04_IRP_P01_QRUNING","CB3_AER04_IRP_P02_QRUNING"],
    "15": ["CB3_BH_RBL_B01_Runing","CB3_BH_RBL_B02_Runing","CB3_BH_RBL_B03_SYS_Run","CB3_BH_RBL_B04_Runing","CB3_SST01_SSP_M01_QRUNING","CB3_SST02_SSP_M02_QRUNING","CB3_SST03_SSP_M03_QRUNING","CB3_SST04_SSP_M04_QRUNING"],
    "16": ["CB3_LPS_SBP_P01_QRUNING","CB3_LPS_SBP_P02_QRUNING","CB3_LPS_SBP_P03_QRUNING","CB3_LPS_SBP_P04_QRUNING"],
    "17": ["CB3_CMF_01_LvPanDianJiRun","CB3_CMF_02_LvPanDianJiRun","CB3_CMF_03_LvPanDianJiRun","CB3_CMF_04_LvPanDianJiRun","CB3_CMF_05_LvPanDianJiRun","CB3_CMF_06_LvPanDianJiRun","CB3_DFP_IRP_P01_QRUNING","CB3_DFP_IRP_P02_QRUNING"],
    "18": ["CB3_HST_CAT_M01_QRUNING","CB3_HST_CAT_M02_QRUNING","CB3_HST_FAT_M01_QRUNING","CB3_HST_FAT_M02_QRUNING","CB3_HST_FAT_M03_QRUNING","CB3_HST_FAT_M04_QRUNING","CB3_HST_SSP_M01_QRUNING","CB3_HST_SSP_M02_QRUNING"],
    "19": ["CB3_HST_RSP_P01_QRUNING","CB3_HST_RSP_P02_QRUNING","CB3_HST_ESP_P01_QRUNING","CB3_HST_ESP_P02_QRUNING"],
    "20": ["CB2_NaClO_VLP_P01_DI_RUN","CB2_NaClO_VLP_P02_DI_RUN","CB2_PAC_VLP_P01_DI_RUN","CB2_PAC_VLP_P02_DI_RUN","CB2_CAD_VLP_P01_DI_RUN","CB2_CAD_VLP_P02_DI_RUN"],
    "21": ["CB2_PAC_VLP_P01_DI_RUN","CB2_PAC_VLP_P02_DI_RUN"],
    "22": ["CB2_CAD_VLP_P01_DI_RUN","CB2_CAD_VLP_P02_DI_RUN"],
}

let hasTotal = false;
for (let i = 0; i < tagMap[label].length; i++) {
    let tagName = tagMap[label][i];
    if (tagName.indexOf("_ACCU") !== -1) {
        hasTotal = true;
        break;
    }
}

$(Datagrid1.id).datagrid({
    data: [],
    fitColumns: false,
    pagination: true,
    nowrap: false,
    pageList: [50, 80, 100, 120, 150], //设置每页记录条数的列表
}); //清空
//自定义返回月的天数
function getDaysInMonth(year, month) {
    month = parseInt(month, 10);
    var temp = new Date(year, month, '0');
    return temp.getDate();
}
//自定义返回增加的n天的日期
function getaddriqi(year, month, day, n) {
    var temp = new Date(year, month - 1, day);
    temp = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * n);
    temp = temp.format('yyyy-MM-dd');
    return temp;
}
let SLRQ = new Date(DateTimeBox1.Value)
let year1 = SLRQ.getFullYear(); //返回所选年份
let month1 = SLRQ.getMonth() + 1; //返回所选月份
let day1 = SLRQ.getDate(); //返回所选天
let getmonthday = getDaysInMonth(year1, month1); //自定义返回月的天数
// 获取到开始时间后面的日期时间框的数据
let startTime;
// 获取到结束时间后面的日期时间框的数据
let endTime;
let endTimeacc;
var endtnum;
var type1;
var maxendtime, maxendtnum;
let currriqi = $System.$Date;
currriqi = currriqi.replace(/\//g, "-");
// 查询间隔时间
//let timebucket = "60m";
let timebucket = 间隔.GetCurrentText() + "m";
let reporttype;
if (报表类型.GetCurrentText() === '日报表') {
    type1 = 0;
    reporttype = "日报表";
    startTime = DateTimeBox1.Value + ' 00:00:05';
    endTime = DateTimeBox1.Value + ' 23:59:59';
    endTimeacc = getaddriqi(year1, month1, day1, 1) + ' 00:00:04';
    if (currriqi === DateTimeBox1.Value) {
        var currTime = new Date();
        endTime = currTime.format('yyyy-MM-dd hh:mm:ss');
        endTimeacc = endTime;
    }
    endtnum = new Date(DateTimeBox1.Value);
    maxendtime = getaddriqi($System.$Year, $System.$Month, $System.$Day, 0);
    maxendtnum = new Date(maxendtime);
} else if (报表类型.GetCurrentText() === '月报表') {
    type1 = 1;
    reporttype = "月报表";
    timebucket = "1d";
    startTime = getaddriqi(year1, month1, 1, 0) + ' 00:00:00';
    endTime = getaddriqi(year1, month1, getmonthday, 0) + ' 23:59:59';
    endTimeacc = getaddriqi(year1, month1, getmonthday, 1) + ' 00:00:04';
    if (year1 == $System.$Year && month1 == $System.$Month) {
        var currTime1 = new Date();
        endTime = currTime1.format('yyyy-MM-dd hh:mm:ss');
        endTimeacc = endTime;
    }
    endtnum = new Date(getaddriqi(year1, month1, getmonthday, 1));
    maxendtime = getaddriqi($System.$Year, $System.$Month, getDaysInMonth($System.$Year, $System.$Month), 1);
    maxendtnum = new Date(maxendtime);
} else if (报表类型.GetCurrentText() === '年报表') {
    type1 = -1;
    reporttype = "年报表";
    timebucket = "1n";
    startTime = getaddriqi(year1, 1, 1, 0) + ' 00:00:00';
    endTime = getaddriqi(year1, 12, 31, 0) + ' 23:59:59';
    endTimeacc = getaddriqi(year1 + 1, 1, 1, 0) + ' 00:00:04';
    if (year1 == $System.$Year && month1 <= $System.$Month) {
        var currTime1 = new Date();
        endTime = currTime1.format('yyyy-MM-dd hh:mm:ss');
        endTimeacc = endTime;
    }
    endtnum = new Date(getaddriqi(year1, 12, 31, 1));
    maxendtime = getaddriqi($System.$Year, 12, 31, 1);
    maxendtnum = new Date(maxendtime);
}
let datatype;
if (数据类型.GetCurrentText() === '瞬时') {
    datatype = 'data_first';
} else if (数据类型.GetCurrentText() === '平均') {
    datatype = 'data_average';
} else if (数据类型.GetCurrentText() === '最大') {
    datatype = 'data_max';
} else if (数据类型.GetCurrentText() === '最小') {
    datatype = 'data_min';
}
if (报表类型.GetCurrentText() === '月报表' || 报表类型.GetCurrentText() === '年报表') {数据类型.SetCurrentText
    datatype = 'data_first';
}
startTime = startTime.replace(/\//g, "-");
endTime = endTime.replace(/\//g, "-");
//判断
if (endtnum > maxendtnum) {
    alert('时间选择错误！');
} else if (datatype === 'data_first') {
    let options;
    if (hasTotal) {
        options = {
            "type": type1,
            "startTime": startTime,
            "endTime": endTimeacc,
            "timebucket": timebucket,
            "tags": tagMap[label],
            "columns": []
        };
    } else {
        options = {
            "type": type1,
            "startTime": startTime,
            "endTime": endTime,
            "timebucket": timebucket,
            "tags": tagMap[label],
            "columns": []
        };
    }
    GetHistorytagForReport(options, function(res) {
        debugger
        datadealfun(res.Body.records)
    })
} else {
    let options;
    if (hasTotal) {
        options = {
            "startTime": startTime,
            "endTime": endTimeacc,
            "timebucket": timebucket,
            "tags": tagMap[label],
            "columns": ["max", "min", "last", "avg"],
            "isDesc": false
        };
    } else {
        options = {
            "startTime": startTime,
            "endTime": endTime,
            "timebucket": timebucket,
            "tags": tagMap[label],
            "columns": ["max", "min", "last", "avg"],
            "isDesc": false
        };
    }
    GetHistorytagForCurve(options, function(res) {
        debugger
        datadealfun(res.Body.records)
    });
}

function datadealfun(res) {
    let dataY = [];
    let arrLen = 0;
    for (const key in res) {
        if (res[key].length > arrLen) {
            arrLen = res[key].length;
        }
    }
    if (hasTotal || endTime === endTimeacc) {
        arrLen = arrLen - 1;
    }
    for (let i = 0; i < arrLen; i++) {
        let dataT = {};
        //max:datamax min:datamin avg:dataaverage
        dataT.time = null;
        for (let j = 0; j < tagMap[label].length; j++) {
            if (dataT.time === null) {
                if (res[tagMap[label][j]] && res[tagMap[label][j]][i] && res[tagMap[label][j]][i].period) {
                    dataT.time = res[tagMap[label][j]][i].period;
                    var sj = new Date(dataT.time);
                    dataT.time = sj.format('hh:mm:00');
                    if (报表类型.GetCurrentText() === '月报表') {
                        dataT.time = sj.format('yyyy-MM-dd');
                    }
                    if (报表类型.GetCurrentText() === '年报表') {
                        dataT.time = sj.format('yyyy-MM');
                    }
                }
            }
            if (res[tagMap[label][j]] && res[tagMap[label][j]][i]) {
                if (tagMap[label][j].indexOf("_ACCU") !== -1) {
                    let thisData = res[tagMap[label][j]][i][datatype] ? parseFloat(res[tagMap[label][j]][i][datatype].toFixed(2)) : 0;
                    let nextData = res[tagMap[label][j]][i + 1][datatype] ? parseFloat(res[tagMap[label][j]][i + 1][datatype].toFixed(2)) : 0;
                    if(thisData === 0 || nextData=== 0){
                        dataT[tagMap[label][j]] = 0;
                    }else{
                        dataT[tagMap[label][j]] = parseFloat((nextData - thisData).toFixed(2));
                    }
                } else {
                    dataT[tagMap[label][j]] = res[tagMap[label][j]][i][datatype] ? parseFloat(res[tagMap[label][j]][i][datatype].toFixed(2)) : 0
                }
            }
        }
        dataY.push(dataT);
    }
    console.log(dataY);
    //计算数据
    //最大值判断函数定义
    Array.prototype.max = function() {
        var max = this[0];
        this.forEach(function(ele, index, arr) {
            if (ele > max) {
                max = ele;
            }
        })
        return max;
    }
    //最小值判断函数定义
    Array.prototype.min = function() {
        var min = this[0];
        this.forEach(function(ele, index, arr) {
            if (ele < min) {
                min = ele;
            }
        })
        return min;
    }
    //最小值
    let minData = {}
    minData.time = '最小值';
    for (let i = 0; i < tagMap[label].length; i++) {
        let dataArr = [];
        for (var iq = 0; iq < dataY.length; iq++) {
            if (dataY[iq][tagMap[label][i]] !== null && dataY[iq][tagMap[label][i]] !== undefined) {
                dataArr.push(dataY[iq][tagMap[label][i]])
            }
        }
        if (dataArr.length > 0) {
            let min = dataArr.min();
            minData[tagMap[label][i]] = min;
        }
    }
    let maxData = {}
    maxData.time = '最大值';
    for (let i = 0; i < tagMap[label].length; i++) {
        let dataArr = [];
        for (var iq = 0; iq < dataY.length; iq++) {
            if (dataY[iq][tagMap[label][i]] !== null && dataY[iq][tagMap[label][i]] !== undefined) {
                dataArr.push(dataY[iq][tagMap[label][i]])
            }
        }
        if (dataArr.length > 0) {
            let max = dataArr.max();
            maxData[tagMap[label][i]] = max;
        }
    }
    let avgData = {}
    avgData.time = '平均值';
    var sumFunc = function(x, y) {
        return x + y;
    }; //求和
    for (let i = 0; i < tagMap[label].length; i++) {
        let dataArr = [];
        for (var iq = 0; iq < dataY.length; iq++) {
            if (dataY[iq][tagMap[label][i]] !== null && dataY[iq][tagMap[label][i]] !== undefined) {
                dataArr.push(dataY[iq][tagMap[label][i]])
            }
        }
        if (dataArr.length > 0) {
            let sum = dataArr.reduce(sumFunc);
            let avg = parseFloat((sum / dataArr.length).toFixed(2));
            avgData[tagMap[label][i]] = avg;
        }
    }
    let cumulativeData = {}
    cumulativeData.time = '累计值';
    var sumFunc = function(x, y) {
        return x + y;
    }; //求和
    for (let i = 0; i < tagMap[label].length; i++) {
        if (tagMap[label][i].indexOf("_ACCU") !== -1) {
            let dataArr = [];
            for (var iq = 0; iq < dataY.length; iq++) {
                if (dataY[iq][tagMap[label][i]] !== null && dataY[iq][tagMap[label][i]] !== undefined) {
                    dataArr.push(dataY[iq][tagMap[label][i]])
                }
            }
            if (dataArr.length > 0) {
                let sum = dataArr.reduce(sumFunc);
                cumulativeData[tagMap[label][i]] = parseFloat(sum.toFixed(2));
            }
        }else{
            cumulativeData[tagMap[label][i]] = null;
        }

    }
    dataY.push(minData);
    dataY.push(maxData);
    dataY.push(avgData);
    if(hasTotal){
        dataY.push(cumulativeData);
    }

    console.log(dataY);
    //数据网格分页显示
    var tmpData = dataY.slice(0, 50);
    $(Datagrid1.id).datagrid({
        data: tmpData,
        fitColumns: false,
        pagination: true,
        nowrap: false,
        pageList: [50, 80, 100, 120, 150], //设置每页记录条数的列表
    });
    var pager = $(Datagrid1.id).datagrid("getPager"); //数据网格分页对象
    pager.pagination({
        total: dataY.length,
        beforePageText: '第', //页数文本框前显示的汉字
        afterPageText: '页     共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录    共 {total} 条记录',
        onSelectPage: function(pageNo, pageSize) {
            var start = (pageNo - 1) * pageSize;
            var end = start + pageSize;
            tmpData = dataY.slice(start, end);
            $(Datagrid1.id).datagrid("loadData", tmpData);
            pager.pagination('refresh', {
                total: dataY.length,
                pageNumber: pageNo,
            });
        }
    });
}